package com.cmsz.shuang.booter.web;

import org.springframework.beans.factory.annotation.Autowired;

import com.cmsz.shuang.booter.service.CacheDemo;
import com.vaadin.annotations.Push;
import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.communication.PushMode;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SpringUI(path = "/pushui")
@Push(PushMode.MANUAL)
@Theme("valo")
public class PushyUI extends UI {
	
	
	@Autowired
	private CacheDemo ca;

	VerticalLayout layout = new VerticalLayout();
	int i = 0;

	// Chart chart = new Chart(ChartType.AREASPLINE);
	// DataSeries series = new DataSeries();

	@Override
	protected void init(VaadinRequest request) {

		layout.addComponent(new Button("1 button"));

		// chart.setSizeFull();
		// setContent(chart);
		//
		// // Prepare the data display
		// Configuration conf = chart.getConfiguration();
		// conf.setTitle("Hot New Data");
		// conf.setSeries(series);

		// Start the data feed thread

		this.setContent(layout);

		new FeederThread().start();

	}

	class FeederThread extends Thread {
		int count = 0;

		@Override
		public void run() {

			for (;;) {
				try {
					Thread.sleep(1000);
					System.out.println("cache test");
					ca.findById(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				i++;

				access(new Runnable() {

					@Override
					public void run() {

						layout.addComponent(new Button(i + " button"));
						push();
					}
				});

				// try {
				//
				// // Update the data for a while
				// while (count < 100) {
				// Thread.sleep(1000);
				//
				// access(new Runnable() {
				// @Override
				// public void run() {
				// double y = Math.random();
				// series.add(
				// new DataSeriesItem(count++, y),
				// true, count > 10);
				// }
				// });
				// }
				//
				// // Inform that we have stopped running
				// access(new Runnable() {
				// @Override
				// public void run() {
				// setContent(new Label("Done!"));
				// }
				// });
				// } catch (InterruptedException e) {
				// e.printStackTrace();
				// }
			}
		}
	}
}
